<template>
    <div class="common-page page-stats-data-export" v-loading.fullscreen="loading">
        <div class="common-page__head">
            <!-- <comp-paths class="route" :paths="['/stats', $route.fullPath]"></comp-paths> -->
            <div class="title">
                <div class="text">{{ $options.title }}</div>
                <el-button type="plain" size="small" icon="el-icon-download" style="margin: 0 12px 0 0" @click="exportClientsHolder()">导出服务专员信息</el-button>
                <el-button type="plain" size="small" icon="el-icon-download" style="margin: 0 12px 0 0" @click="exportClientsOfficer()">导出后援服务单位信息</el-button>
            </div>
        </div>
        <div class="common-page__body page-stats-data-export__body"></div>
    </div>
</template>

<script>
    import mixin from "../mixin";
    import { string, int } from "../util";
    export default {
        name: "page-stats-data-export",
        title: "数据导出",
        nodes: ["/menu/stats"],
        childs: [],
        mixins: [mixin],
        data: () => ({
            loading: false,
        }),
        methods: {
            exportClientsHolder() {
                if (this.loading) return;
                let content = " <span style='color:red;'>服务专员信息</span> ";
                let callback = () => {
                    this.loading = true;
                    this.plugins.user.exportClientsHolder().then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.alert("导出任务创建成功！", "任务执行需要花费一些时间，可稍后再点击右上角打开“系统任务”查看任务状态。", "success", ["查看任务状态", "稍后查看"]).then((ok) => ok && this.$router.push("/user/tasks"));
                        } else {
                            this.plugins.modal.alert("导出失败", res.code + "：" + res.msg, "error");
                        }
                    });
                };
                this.plugins.modal.alert("批量导出", "确定导出" + content + "吗？", "warning", ["确定导出", "再想想"]).then((ok) => ok && callback());
            },
            exportClientsOfficer() {
                if (this.loading) return;
                let content = " <span style='color:red;'>后援服务单位信息</span> ";
                let callback = () => {
                    this.loading = true;
                    this.plugins.user.exportClientsOfficer().then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.alert("导出任务创建成功！", "任务执行需要花费一些时间，可稍后再点击右上角打开“系统任务”查看任务状态。", "success", ["查看任务状态", "稍后查看"]).then((ok) => ok && this.$router.push("/user/tasks"));
                        } else {
                            this.plugins.modal.alert("导出失败", res.code + "：" + res.msg, "error");
                        }
                    });
                };
                this.plugins.modal.alert("批量导出", "确定导出" + content + "吗？", "warning", ["确定导出", "再想想"]).then((ok) => ok && callback());
            },
            destroy() {
                if (this.loading) return;
                return true;
            },
            init() {
                if (this.loading) return;
                this.loading = true;
                this.$root.$refs.navbar.init();
                this.plugins.user.call(async () => {
                    let res = await this.plugins.user.init();
                    if (res.code === 0) { }
                    return res;
                }, async () => {
                    if (this.access(this.$route.name)) return { code: 0 };
                    return this.env.xhrErrors.find((error) => error.code === 4);
                }).then((res) => {
                    this.loading = false;
                    this.$root.$refs.navbar.init();
                    if (res.code === 0) {

                    } else if (res.code === 2) {
                        this.$root.$refs.navbar.login();
                    } else {
                        this.plugins.modal.alert("页面加载失败", res.code + "：" + res.msg, "error").then(() => this.$router.replace("/home"));
                    }
                });
            },
        },
    };
</script>

<style lang="scss">
    .page-stats-data-export {
        overflow: hidden;
    }

    .page-stats-data-export__body {
        display: flex;
        flex-direction: column;
    }
</style>